package com.huiyou.service.serviceImpl;

import com.huiyou.common.utils.TimeUtils;
import com.huiyou.item.pojo.Brand;
import com.huiyou.item.pojo.WpMember;
import com.huiyou.item.pojo.WpMemberMoneylog;
import com.huiyou.item.pojo.WpOrderPf;
import com.huiyou.item.service.CoreOrderService;
import com.huiyou.service.mapper.WpMemberMapper;
import com.huiyou.service.mapper.WpMemberMoneylogMapper;
import com.huiyou.service.mapper.WpOrderPfMapper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class CoreOrderServiceImpl implements CoreOrderService {

    @Autowired
    private WpMemberMapper wpMemberMapper;

    @Autowired
    private WpMemberMoneylogMapper wpMemberMoneylogMapper;

    @Autowired
    private WpOrderPfMapper wpOrderPfMapper;



    @Override
    public void testOrder() {
        /*Example example = new Example(Brand.class);
        example.createCriteria().andGreaterThanOrEqualTo("date","2020-01-05")
                .andLessThanOrEqualTo("date","2020-01-03");
        List<WpOrderPf> list = wpOrderPfMapper.selectByExample(example);*/
        List<WpOrderPf> list =  wpOrderPfMapper.findByDate("2020-01-05","2020-01-04");
      /*  for (int i = 0; i < list.size() ; i++) {
            WpOrderPf wpOrderPf = list.get(i);
            Integer uid = wpOrderPf.getUid();
            if(wpOrderPf.getIsok()==1) {
                Example example = new Example(WpMemberMoneylog.class);
                example.createCriteria().andEqualTo("uid",uid);
                example.setOrderByClause(" id desc");
                List<WpMemberMoneylog> logsList = wpMemberMoneylogMapper.selectByExample(example);
                WpMemberMoneylog wpMemberMoneylog = logsList.get(0);
                BigDecimal logAgoMoney = wpMemberMoneylog.getLogAgoMoney();
                BigDecimal logMoney = wpMemberMoneylog.getLogMoney();

                WpMember wpMember = wpMemberMapper.selectByPrimaryKey(uid);
                wpMember.setMoney(logAgoMoney);
                //更新金额
                wpMemberMapper.updateByPrimaryKey(wpMember);
                //删除返现日志
                wpMemberMoneylogMapper.delete(wpMemberMoneylog);

                //还原返现状态
                wpOrderPf.setIsok(0);
                wpOrderPfMapper.updateByPrimaryKey(wpOrderPf);
            }




        }
*/
        System.out.println(list.size());
    }

    @Override
    public void testGetPfOrderMoney() {
        String startData = "2020-01-31 0:00:00";
        String endData =   "2020-02-01 0:00:00";
        //根据时间查找对应的批发区订单
        List<WpOrderPf> list = wpOrderPfMapper.findOrderPfByData(startData, endData);
        for (int i = 0; i < list.size(); i++) {
            WpOrderPf wpOrderPf = list.get(i);
            Integer uid = wpOrderPf.getUid();
            if (wpOrderPf.getIsjs() == 1 && wpOrderPf.getIspay() == 1 && wpOrderPf.getIsok() == 0) {
                //根据uid查询客户详情
                WpMember wpMember = wpMemberMapper.selectByPrimaryKey(uid);
                BigDecimal money = wpMember.getMoney();
                //返现金额
                BigDecimal cashback = wpOrderPf.getCashback();
                //添加账户余额日志
                WpMemberMoneylog wpMemberMoneylog = new WpMemberMoneylog();
                wpMemberMoneylog.setLogAgoMoney(money);
                wpMemberMoneylog.setLogMoney(cashback);
                wpMemberMoneylog.setUid(uid);
                wpMemberMoneylog.setLogScene("jsfx");
                wpMemberMoneylog.setLogType("inc");
                wpMemberMoneylog.setLogInfo("寄售返现");
                wpMemberMoneylog.setLogField("money");
                wpMemberMoneylog.setCtime(TimeUtils.DateToTimestamp(new Date()));
                wpMemberMoneylogMapper.insert(wpMemberMoneylog);

                //更新金额
                BigDecimal moneyAfter = money.add(cashback);
                wpMember.setMoney(moneyAfter);
                //寄售已返现
                BigDecimal moneyFxO = wpMember.getMoneyFxO();
                BigDecimal moneyFxOAfter = moneyFxO.add(cashback);
                wpMember.setMoneyFxO(moneyFxOAfter);
                //寄售未返现
                BigDecimal moneyFxW = wpMember.getMoneyFxW();
                BigDecimal moneyFxWAfter = moneyFxW.subtract(cashback);
                wpMember.setMoneyFxW(moneyFxWAfter);
                //更新
                wpMemberMapper.updateByPrimaryKey(wpMember);

                //更新返现状态,返现时间
                wpOrderPf.setIsok(1);
                wpOrderPf.setFtime(TimeUtils.DateToTimestamp(new Date()));
                wpOrderPfMapper.updateByPrimaryKey(wpOrderPf);
            }
            System.out.println(uid+"--------------"+list.size());

        }
        System.out.println("--------------系统订单返现成功");
    }


}
